package com.qf.agro.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.pojo.SysModule;
import com.qf.agro.service.SysModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.StreamInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ProjectName: smart-agriculture-parent
 * ClassName: SysModuleController
 * Author: chenyaosen
 * Date: 2023/7/4 14:30
 * Description:
 */
@RestController
@RequestMapping("/sys/module")
public class SysModuleController {

    @Autowired
    private SysModuleService moduleService;

    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, String name) {
        Page<SysModule> page = moduleService.findPage(pageNum,pageSize,name);
        return ResultInfo.success(page);
    }

    @GetMapping("/list")
    public ResultInfo findModuleList() {
        List<SysModule> modules = moduleService.findAll();
        return ResultInfo.success(modules);
    }

    @GetMapping("/roleModule")
    public ResultInfo findRoleModuleList(Long roleId) {
        List<Long> roleModule = moduleService.findRoleModule(roleId);
        return ResultInfo.success(roleModule);
    }

    @PostMapping("/roleModule")
    public ResultInfo updateRoleModule(@RequestBody Map<String,Object>map) {
        long roleId = Long.parseLong((String) map.get("roleId"));
        List<Long> moduleIds = (List<Long>) map.get("moduleIds");
        moduleService.updateRoleModule(roleId,moduleIds);
        return ResultInfo.success(null);
    }
}
